﻿using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using Nexterday.Linq.Autocompiler;

namespace Linq.Autocompiler.EF4
{
    public class ConcurrentDictionaryCache : ICompiledQueryCache
    {
        static ConcurrentDictionary<string, Delegate> _cache = new ConcurrentDictionary<string, Delegate>();
        
        public static ConcurrentDictionaryCache Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }

            internal static readonly ConcurrentDictionaryCache instance = new ConcurrentDictionaryCache();
        }

        public ConcurrentDictionaryCache()
        {
        }

        
        #region ICompiledQueryCache Members

        public Delegate GetOrAdd<TResult>(string key, Func<string,Delegate> valueFactory)
        {
            return _cache.GetOrAdd(key, valueFactory);
        }

        #endregion
    }
}
